package org.poptest.junit4;

import org.junit.internal.runners.model.ReflectiveCallable;
import org.junit.runners.model.FrameworkMethod;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @author Alan Roche
 *         Date: 24/01/11
 *         Time: 23:56
 */
public class ParameterizedFrameworkMethod  extends FrameworkMethod {
    private final Method method;
    private final Class<?> keyClass;

    public ParameterizedFrameworkMethod(final Class<?> keyClass, final Method method) {
        super(method);
        this.method = method;
        this.keyClass = keyClass;
    }

    public ParameterizedFrameworkMethod(final Class<?> keyClass, final FrameworkMethod method) {
        super(method.getMethod());
        this.method = method.getMethod();
        this.keyClass = keyClass;
    }

    /**
     * Doesn't validate the no-args part!
     * Allows args, but will pass
     */
    public void validatePublicVoidNoArg(boolean isStatic, final List<Throwable> errors) {
        validatePublicVoid(isStatic, errors);
    }

    public Object invokeExplosively(final Object target, final Object... params)
            throws Throwable {

        // Just passes null args
        final Object[] args = new Object[method.getParameterTypes().length];


        return new ReflectiveCallable() {
            @Override
            protected Object runReflectiveCall() throws Throwable {
                return method.invoke(target, args);
            }
        }.run();
    }
}
